package com.design.cjm.structural.composite.example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: Jie Ming Chen
 * @Date: 2018/9/24
 * @Version 1.0
 */
public class Composite extends Component{

    private List<Component> children = new ArrayList<>();

    public Composite(String name) {
        super(name);
    }

    @Override
    public void add(Component c) {

        children.add(c);
    }

    @Override
    public void remove(Component c) {

        Iterator<Component> it = children.iterator();
        while (it.hasNext()) {

            if (it.next().equals(c)) {
                it.remove();
            }
        }
    }

    @Override
    public void display(int depth) {

        System.out.println("-" + depth + name);

        for (Component component: children) {

            component.display(depth + 2);
        }
    }
}
